/**
 * meiguang8.com
 * Copyright (C) 2016-2019 All Rights Reserved.
 */
package com.yuanchuang.yp.mall.web.admin.common.config.spring;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
/**
 * class_name: SpringHelper
 * package: com.yuanchuang.yp.mall.web.admin.common.config.spring
 * describe: TODO
 * @author: yuanyaun
 * creat_date: 2019/7/16
 * creat_time: 15:29
 **/
@Configuration
@Data
public class SpringHelper implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Value("${aesEncodeKey}")
    public  String aesEncodeKey;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringHelper.applicationContext = applicationContext;
    }

    private static <T> T getBean(Class<T> type) {
        return applicationContext.getBean(type);
    }

    /***
     * 获取目前运行的环境变量
     * @return
     */
    private static Environment getEnvironment() {
        return getBean(Environment.class);
    }

    private static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }


    @PostConstruct
    public static String getStaticAesEncodeKey() {
        SpringHelper bean = applicationContext.getBean(SpringHelper.class);
        return bean.getAesEncodeKey();
    }
}
